/*
 *   Copyright 2008 Marcos Silva Pereira
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

class DataSetInfoTests extends GroovyTestCase {

    protected void setUp() {
        File.metaClass.listFiles = {FilenameFilter filter ->
            def files = []
            files << new File(delegate, 'dataset1.xml')
            files << new File(delegate, 'dataset2.xml')
            return files as File[]
        }
    }

    protected void tearDown() {
        def remove = GroovySystem.metaClassRegistry.&removeMetaClass
        remove java.io.File
    }

    public void testInfoFactoryMethodShouldReturnsACleanDataSetInfo() {
        def info = DataSetInfo.info()
        assertNull info.className
        assertNull info.datasets
        assertNull info.datasetHomeDirectory
    }
    
    public void testShouldNotFailWhenTheClassDoesNotHaveAStaticDataSetAttribute() {
    	def info = DataSetInfo.info().forClass(DataSetInfoTests.class)
    	def files = info.getAllDatasetFiles()
    	assertTrue "Data set file list must be empty", files.empty
    }

    public void testShouldChangeHomeDirectoryForDataSets() {
        def info = DataSetInfo.info().withDataSetHome('another/directory')
        assertEquals file('another/directory'), info.datasetHomeDirectory
    }

    public void testChangeHomeDirectoryForDataSetsShouldFailWhenPassingNull() {
        shouldFail(IllegalArgumentException) {
            DataSetInfo.info().withDataSetHome(null)
        }
    }

    public void testShouldListAllFilesForAGivenDataset() {

        def info = info(['dataset'])
        def files = info.getAllDatasetFiles()

        assertEquals 2, files.size()

        assertTrue files.contains(file("/home/dataset/dataset1.xml"))
        assertTrue files.contains(file("/home/dataset/dataset2.xml"))
        
    }

    public void testShouldListAllFilesWhenThereIsMoreThanOneDataSet() {

        def info = info(['dataset1', 'dataset2'])
        def files = info.getAllDatasetFiles()

        assertEquals 4, files.size()

        assertTrue files.contains(file("/home/dataset1/dataset1.xml"))
        assertTrue files.contains(file("/home/dataset1/dataset2.xml"))

        assertTrue files.contains(file("/home/dataset2/dataset1.xml"))
        assertTrue files.contains(file("/home/dataset2/dataset2.xml"))

    }
    
    public void testShouldListAllFilesWhenUsingADatasetWithNestedDirectory() {
    	
    	def info = info(['dataset/inner'])
    	def files = info.getAllDatasetFiles()
    	
    	assertEquals 2, files.size()
    	
    	assertTrue files.contains(file("/home/dataset/inner/dataset1.xml"))
    	assertTrue files.contains(file("/home/dataset/inner/dataset2.xml"))
    }
    
    def file(name) {
    	new File(name)
    }

    def info(datasets) {
        new DataSetInfo(datasetHomeDirectory:new File('/home/'), 'datasets':datasets)
    }

}
